#include "GlTools.h"

#include <GL/gl.h>

/*
	GlTools
*/

/*	public	*/

void GlTools::modelCuboid(float dx, float dy, float dz) {

	glBegin(GL_QUADS);

	// top
	glNormal3f(0.0, 1.0, 0.0);
	glVertex3f(-dx,  dy, -dz);
	glVertex3f( dx,  dy, -dz);
	glVertex3f( dx,  dy,  dz);
	glVertex3f(-dx,  dy,  dz);


	// bottom
	glNormal3f(0.0, -1.0, 0.0);
	glVertex3f(-dx, -dy, -dz);
	glVertex3f( dx, -dy, -dz);
	glVertex3f( dx, -dy,  dz);
	glVertex3f(-dx, -dy,  dz);

	// left
	glNormal3f(-1.0, 0.0, 0.0);
	glVertex3f(-dx,  dy, -dz);
	glVertex3f(-dx,  dy,  dz);
	glVertex3f(-dx, -dy,  dz);
	glVertex3f(-dx, -dy, -dz);

	// right
	glNormal3f(1.0, 0.0, 0.0);
	glVertex3f( dx,  dy, -dz);
	glVertex3f( dx,  dy,  dz);
	glVertex3f( dx, -dy,  dz);
	glVertex3f( dx, -dy, -dz);

	// front 
	glNormal3f(0.0, 0.0, -1.0);
	glVertex3f(-dx,  dy, -dz);
	glVertex3f( dx,  dy, -dz);
	glVertex3f( dx, -dy, -dz);
	glVertex3f(-dx, -dy, -dz);

	// back
	glNormal3f(0.0, 0.0, 1.0);
	glVertex3f(-dx,  dy,  dz);
	glVertex3f( dx,  dy,  dz);
	glVertex3f( dx, -dy,  dz);
	glVertex3f(-dx, -dy,  dz);

	glEnd();
}

void GlTools::renderCheckerPlane(float size, int xCount, int zCount) {
	glBegin(GL_QUADS);
	for(int i = 0; i < xCount; ++i) {
		for(int j = 0; j < zCount; ++j) {

			if ((i + j) % 2 == 0) {
				glColor4f(1.0, 1.0, 1.0, 1.0);
			} else {
				glColor4f(0.0, 0.0, 0.0, 1.0);
			}

			float di = (float)i - (float)xCount / 2.0;
			float dj = (float)j - (float)zCount / 2.0;

			glNormal3f(0.0, 1.0, 0.0);
			glVertex3f((float)di * size, 0.0, (float)dj * size);
			glVertex3f((float)(di + 1) * size, 0.0, (float)dj * size);
			glVertex3f((float)(di + 1) * size, 0.0, (float)(dj + 1) * size);
			glVertex3f((float)di * size, 0.0, (float)(dj + 1) * size);
		}
	}

	glEnd();
}

